﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;

    public class ShippingUDF : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected HtmlForm form1;
        protected GridView gvOrderDetailUdf;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOrderID;
        protected Label lblMsg;
        protected Label lblShippingUDF;
        protected HtmlLink mycss;
        protected Label one1;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("Shipping.aspx?from={0}&Tab={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "4", base.UrlEncode(this.hdnOrderID.Value)), false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                List<OrderDetailInfo> orderDetailInfo = base.DataEntityViewState as List<OrderDetailInfo>;
                if (this.gvOrderDetailUdf.Rows.Count > 0)
                {
                    for (int i = 0; this.gvOrderDetailUdf.Rows.Count > i; i++)
                    {
                        foreach (OrderDetailInfo info in orderDetailInfo)
                        {
                            if (this.gvOrderDetailUdf.Rows[i].Cells[1].Text == info.LineID.ToString())
                            {
                                info.Udf1 = ((TextBox) this.gvOrderDetailUdf.Rows[i].FindControl("txtUdf1")).Text;
                                info.Udf2 = ((TextBox) this.gvOrderDetailUdf.Rows[i].FindControl("txtUdf2")).Text;
                                info.Remark = ((TextBox) this.gvOrderDetailUdf.Rows[i].FindControl("txtRemark")).Text;
                            }
                        }
                    }
                    if (orderDetailInfo.Count > 0)
                    {
                        string result = OutboundServiceClientProxy.GetInstance().UpdateShippUDFByOrderID(orderDetailInfo);
                        this.lblMsg.Text = "行号 " + result + " 更新成功！";
                        ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "ShippingPage", "<script language='javascript'>windowClose();</script>", false);
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "操作异常:" + ex.Message;
            }
        }

        protected void gvOrderDetailUdf_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvOrderDetailUdf.PageIndex * this.gvOrderDetailUdf.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
            }
        }

        private void LoadData()
        {
            this.hdnOrderID.Value = base.Request.QueryString["OrderID"];
            this.hdnFrom.Value = base.Request.QueryString["From"];
            if (this.Session["ShippingUdf"] != null)
            {
                List<OrderDetailInfo> orderDetailInfo = this.Session["ShippingUdf"] as List<OrderDetailInfo>;
                if ((orderDetailInfo != null) && (orderDetailInfo.Count > 0))
                {
                    base.DataEntityViewState = orderDetailInfo;
                    this.Session["ShippingUdf"] = "";
                    this.gvOrderDetailUdf.DataSource = orderDetailInfo;
                    this.gvOrderDetailUdf.DataBind();
                }
                else
                {
                    this.lblMsg.Text = "数据源无效,请关闭页面后从新操作！";
                }
            }
            else
            {
                this.lblMsg.Text = "数据源无效,请关闭页面后从新操作！";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Response.Buffer = true;
            base.Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1.0);
            base.Response.Expires = 0;
            base.Response.CacheControl = "no-cache";
            base.Response.AddHeader("Pragma", "No-Cache");
            if (!base.IsPostBack)
            {
                this.LoadData();
            }
        }
    }
}

